package com.industrial.premu.service.impl;

import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.domain.PreMuCompanyInfo;
import com.industrial.premu.domain.PreMuCompanyInfoNo;
import com.industrial.premu.domain.PreMuCompanyRename;
import com.industrial.premu.mapper.*;
import com.industrial.premu.service.IPreMuCompanyRenameService;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PreMuCompanyRenameServiceImpl implements IPreMuCompanyRenameService {

    @Value("${file.pre}")
    private String filePre;

    @Autowired
    private PreMuCompanyRenameMapper preMuCompanyRenameMapper;

    @Autowired
    private PreMuCompanyInfoMapper companyInfoMapper;

    @Autowired
    private PreMuCompanyInfoNoMapper preMuCompanyInfoNoMapper;


    @Autowired
    private PreMuBenefitEvaluationMapper preMuBenefitEvaluationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询企业更名
     *
     * @param id 企业更名主键
     * @return 企业更名
     */
    @Override
    public PreMuCompanyRename selectPreMuCompanyRenameById(String id)
    {
        return preMuCompanyRenameMapper.selectPreMuCompanyRenameById(id);
    }

    /**
     * 查询企业更名列表
     *
     * @param preMuCompanyRename 企业更名
     * @return 企业更名
     */
    @Override
    public List<PreMuCompanyRename> selectPreMuCompanyRenameList(PreMuCompanyRename preMuCompanyRename)
    {
        String username = SecurityUtils.getUsername();

        PreMuCompanyInfo companyInfo = companyInfoMapper.selectPreMuCompanyInfoByUscc(username);
        if(companyInfo == null){
            PreMuCompanyInfoNo companyInfoNo = preMuCompanyInfoNoMapper.selectPreMuCompanyInfoNoByUscc(username);
            if(companyInfoNo != null){
                preMuCompanyRename.setCompanyId(companyInfoNo.getId());
            }
        }else{
            preMuCompanyRename.setCompanyId(companyInfo.getId());
        }
        return preMuCompanyRenameMapper.selectPreMuCompanyRenameList(preMuCompanyRename);
    }

    /**
     * 新增企业更名
     *
     * @param preMuCompanyRename 企业更名
     * @return 结果
     */
    @Override
    public int insertPreMuCompanyRename(PreMuCompanyRename preMuCompanyRename)
    {
        return preMuCompanyRenameMapper.insertPreMuCompanyRename(preMuCompanyRename);
    }

    /**
     * 修改企业更名
     *
     * @param preMuCompanyRename 企业更名
     * @return 结果
     */
    @Override
    public int updatePreMuCompanyRename(PreMuCompanyRename preMuCompanyRename)
    {
        return preMuCompanyRenameMapper.updatePreMuCompanyRename(preMuCompanyRename);
    }

    /**
     * 批量删除企业更名
     *
     * @param ids 需要删除的企业更名主键
     * @return 结果
     */
    @Override
    public int deletePreMuCompanyRenameByIds(String[] ids)
    {
        return preMuCompanyRenameMapper.deletePreMuCompanyRenameByIds(ids);
    }

    /**
     * 删除企业更名信息
     *
     * @param id 企业更名主键
     * @return 结果
     */
    @Override
    public int deletePreMuCompanyRenameById(String id)
    {
        return preMuCompanyRenameMapper.deletePreMuCompanyRenameById(id);
    }

    /**
     * <p>
     * 更改名称
     * </p>
     *
     * @param companyRename
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void chanage(PreMuCompanyRename companyRename) {
        if(StringUtils.isBlank(companyRename.getCompanyId())){
            throw new RuntimeException("没有找到企业ID");
        }

        PreMuCompanyInfo companyInfo1 = companyInfoMapper.selectPreMuCompanyInfoById(companyRename.getCompanyId());
        if(companyInfo1 != null){
            PreMuCompanyInfo companyInfo = new PreMuCompanyInfo();
            companyInfo.setId(companyRename.getCompanyId());
            companyInfo.setCompanyName(companyRename.getNewName());
            companyInfoMapper.updatePreMuCompanyInfo(companyInfo);

            PreMuBenefitEvaluation preMuBenefitEvaluation = new PreMuBenefitEvaluation();
            preMuBenefitEvaluation.setCompanyId(companyRename.getCompanyId());
            preMuBenefitEvaluation.setCompanyName(companyRename.getNewName());
            //修改用户的NickName
            SysUser sysUser = new SysUser();
            sysUser.setUserId(SecurityUtils.getUserId());
            sysUser.setNickName(companyRename.getNewName());
            sysUserMapper.updateNickNameBuUserName(sysUser);
            preMuBenefitEvaluationMapper.updatePreMuBenefitEvaluationCompanyNameByCompanyId(preMuBenefitEvaluation);
        }else{
            PreMuCompanyInfoNo preMuCompanyInfoNo = preMuCompanyInfoNoMapper.selectPreMuCompanyInfoNoById(companyRename.getCompanyId());
            if(preMuCompanyInfoNo == null){
                throw new RuntimeException("没有找到企业信息");
            }
            PreMuCompanyInfoNo companyInfoNo = new PreMuCompanyInfoNo();
            companyInfoNo.setId(companyRename.getCompanyId());
            companyInfoNo.setCompanyName(companyRename.getNewName());
            preMuCompanyInfoNoMapper.updatePreMuCompanyInfoNo(companyInfoNo);

            //修改用户的NickName
            SysUser sysUser = new SysUser();
            sysUser.setUserName(companyRename.getUscc());
            sysUser.setNickName(companyRename.getNewName());
            sysUserMapper.updateNickNameBuUserName(sysUser);
        }
        //保存更改记录
        companyRename.setId(IdUtil.simpleUUID());
        if(StringUtils.isNotBlank(companyRename.getFilePath())){
            String filePath = companyRename.getFilePath();
            String newFilePath = filePath.replace(filePre, "/file");
            companyRename.setFilePath(newFilePath);
        }
        companyRename.setCreateDate(DateUtils.getNowDate());
        preMuCompanyRenameMapper.insertPreMuCompanyRename(companyRename);
    }
}
